<template>
  <div v-loading="loading">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:allround:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle()"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>
    <el-table :data="dealAllRoundList" tooltip-effect="darkTable" :span-method="objectSpanMethod" class="noneHoverBg" border>
      <el-table-column :label="$t('尽调清单大类')" align="center" prop="categoriesType" :formatter="categoriesTypeFormat" />
      <el-table-column :label="$t('尽调清单子类')" align="center" prop="categoriesChildTypeFormat" />
      <el-table-column :label="$t('修改时间')" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:allround:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="$t('尽调项')" :visible.sync="formOpen" width="60%" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealAllRoundIndexForm ref="dealAllRoundIndexForm" />
    </el-dialog>
  </div>
</template>

<script>
  import { dealAllRoundList, deleteByCategoriesChildType } from '@/api/deal/dd/dealDd'
  import dealAllRoundIndexForm from '@/views/deal/allround/dealAllRoundIndexForm'
  export default {
    components: {
      dealAllRoundIndexForm
    },
    data() {
      return {
        // 遮罩层
        loading: false,
        // 全面尽调表格数据
        dealAllRoundList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        formOpen: false,
        form: {},
        queryParams: {
        },
        // 尽调清单大类
        categoriesTypeOptions: [],
        categoriesTypeChildrenOptions: []
      }
    },
    created() {
      this.getDicts('dd_categories_type').then(response => {
        this.categoriesTypeOptions = response.data
      })
    },
    methods: {
      reset() {
        this.form = {
          id: undefined,
          dealId: this.$route.query.id,
          sort: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        }
        this.resetForm('form')
      },
      // 根据基金id查询
      initIndex(dealId) {
        this.loading = true
        this.dealAllRoundList = []
        dealAllRoundList({ 'dealId': dealId }).then(response => {
          this.dealAllRoundList = response.data
          this.dealAllRoundList.forEach(item => {
            item.dealId = dealId
          })
          this.loading = false
        })
      },
      addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealAllRoundIndexForm.init(null)
        })
      },
      updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
          this.$refs.dealAllRoundIndexForm.init(row)
        })
      },
      /** 删除按钮操作 */
      deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          return deleteByCategoriesChildType(row.categoriesChildType, row.dealId)
        }).then(() => {
          this.initIndex(row.dealId)
          this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
      },
      cancel() {
        this.$parent.$parent.ddListIndexOpen = false
        this.$parent.$parent.getList()
      },
      // 尽调清单大类字典翻译
      categoriesTypeFormat(row) {
        return this.selectDictLabel(this.categoriesTypeOptions, row.categoriesType)
      },
      rowspan(data) {
        // 二维数组，用于存放单元格合并规则
        const spanArr = []
        // 用于存储相同项的开始index
        let position = 0
        data.forEach((item, index) => {
          if (index === 0) {
            spanArr.push(1)
            position = 0
          } else {
            if (data[index].categoriesType === data[index - 1].categoriesType) {
              spanArr[position] += 1 // 有相同项
              spanArr.push(0) // 名称相同后往数组里面加一项0
            } else {
              spanArr.push(1) // 同列的前后两行单元格不相同
              position = index
            }
          }
        })
        return spanArr
      },
      objectSpanMethod({ rowIndex, columnIndex }) {
        if (columnIndex === 0) {
          const spanArr = this.rowspan(this.dealAllRoundList)
          for (let i = 0; i < spanArr.length; i++) {
            const _row = spanArr[rowIndex]
            return {
              rowspan: _row,
              colspan: 1
            }
          }
        }
      }
    }
  }
</script>

<style scoped>

</style>
